package gamebackend;

import framework.Color;
import java.util.ArrayList;
import java.util.List;

import framework.GraphicsWrapper;

/**
 * A tiny bit of exploded block.
 * @author Jonathan Caddey
 *
 */
public class BlockPart {
	private static int DEFAULT_LIFE_TIME = 150;
	private float my_x;
	private float my_y;
	public float my_width;
	public float my_height;
	public int my_life;
	private final Color my_color;
	
	/**
	 * Creates an explosion of BlockParts assuming a block was hit by the ship.
	 * @param shipX the x location of the ship that ran into the block.
	 * @param shipY
	 * @param blockX the top right corner of the block hit by the ship.
	 * @param blockY
	 * @param blockWidth the width of the block.
	 * @param blockHeight
	 * @return
	 */
	public static List<BlockPart> explodeBlock(double shipX, double shipY, float blockX,
			float blockY, float blockWidth, float blockHeight, Color color) {
		List<BlockPart> parts = new ArrayList<BlockPart>();
		parts.add(new BlockPart(blockX, blockY, blockWidth, blockHeight, color, DEFAULT_LIFE_TIME));
		return parts;
	}
	
	public BlockPart(final float x, final float y, final float width, final float height,
			Color color, int lifeTime) {
		my_x = x;
		my_y = y;
		my_width = width;
		my_height = height;
		my_life = lifeTime;
		my_color = color;
	}
	public void step(int timeDelta, double yCameraChange) {
		my_y += yCameraChange;
		if (my_life > 0) {
			my_life -= timeDelta;
		}
		
	}
	public void render(GraphicsWrapper g) {
		Color color = new Color(my_color.getRed(), my_color.getGreen(), my_color.getBlue(),
				Math.min(255, (int) (255 * 2 * my_life / DEFAULT_LIFE_TIME)));
		g.setColor(color);
		g.fillRect((int)my_x, (int)my_y, (int)my_width, (int)my_height);
	}
	
	public boolean isAlive() {
		return my_life > 0;
	}

}
